package com.webserver.core;

import com.webserver.controller.UserController;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
/**
* 用于处理请求
* */
public class DispatcherServlet {
    public void service(HttpRequest request, HttpResponse response){
        String path = request.getRequestURI();

        //首先判断该请求是否为请求一个业务
        if("/myweb/registerUser".equals(path)){
            //处理注册
            UserController controller = new UserController();
            controller.register(request,response);
        }else if ("/myweb/loginUser".equals(path)) {
            UserController controller = new UserController();
            controller.login(request,response);
        }else {
            File file = new File("./webapps"+path);
            if (file.exists()&&file.isFile()) {
                /*
                HTTP/1.1 200 OK(CRLF)
                Content-Type: text/html(CRLF)
                Content-Length: 2546(CRLF)(CRLF)
                1011101010101010101......
                */
                response.setEntity(file);
                System.out.println("path:"+path);
             } else {
                response.setStatusCode(404);
                response.setStatusReason("Notfound");
                file = new File("./webapps/root/404.html");
                response.setEntity(file);
            }

         }
        //该响应头是否告知浏览器服务端是谁
        response.putHeader("Server","WebServer");


    }


}
